package com.nmdl.test2;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class DevicesAdapter extends RecyclerView.Adapter<DevicesAdapter.ViewHolder> {
    List<BluetoothDevice> bluetoothDeviceList = new ArrayList<>();
    ItemClickListener itemclickListener;
    BluetoothAdapter blueToothClient;
    Context context;

    public DevicesAdapter(Context context, BluetoothAdapter blueToothClient, ItemClickListener itemclickListener) {
        this.context = context;
        this.blueToothClient = blueToothClient;
        this.itemclickListener = itemclickListener;
    }

    public void updateDevice(BluetoothDevice bluetoothDevice) {
        Boolean contains = false;
        for (BluetoothDevice device : bluetoothDeviceList) {
            if (device.getAddress().equals(bluetoothDevice.getAddress())) {
                Collections.replaceAll(bluetoothDeviceList, device, bluetoothDevice);
                contains = true;
                break;
            }
        }
        if (!contains) bluetoothDeviceList.add(bluetoothDevice);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_devices, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        BluetoothDevice mBlueDevice = bluetoothDeviceList.get(position);
        holder.device_name_tv.setText("名称: " + mBlueDevice.getName());
        holder.mac_id_tv.setText("MAC: " + mBlueDevice.getAddress());
        switch (mBlueDevice.getBondState()) {
            case BluetoothDevice.BOND_NONE://未连接
                setDeviceColor(holder, Color.GRAY);
                holder.connect_device_btn.setText("未连接");
                holder.connect_device_btn.setOnClickListener(view -> {
                    if (itemclickListener != null)
                        itemclickListener.bondClick(mBlueDevice);
                });
                break;
            case BluetoothDevice.BOND_BONDING://正在配对
                setDeviceColor(holder, Color.LTGRAY);
                holder.connect_device_btn.setText("正在配对");
                break;
            case BluetoothDevice.BOND_BONDED://已配对
                setDeviceColor(holder, Color.GREEN);
                holder.connect_device_btn.setText("已配对");
                holder.connect_device_btn.setOnClickListener(view -> {
                    if (itemclickListener != null)
                        itemclickListener.connectClick(mBlueDevice);
                });
                setConnectStyle(holder, mBlueDevice);//再次判断是否已连接
                break;
        }
        if (mBlueDevice.getBondState() == BluetoothDevice.BOND_BONDED) {//绑定状态下才让点击Item
            holder.device_layout.setOnClickListener(view -> {
                if (itemclickListener != null)
                    itemclickListener.itemClick(mBlueDevice);
            });
        }
    }

    private void setConnectStyle(ViewHolder holder, BluetoothDevice mBlueDevice) {
        boolean connected = BleHelper.isDeviceConnect(blueToothClient, mBlueDevice);
        if (!connected) return;
        holder.connect_device_btn.setText("已连接");
        holder.connect_device_btn.setEnabled(false);
    }

    private void setDeviceColor(ViewHolder holder, int color) {
        holder.device_name_tv.setTextColor(color);
        holder.mac_id_tv.setTextColor(color);
    }

    @Override
    public int getItemCount() {
        return bluetoothDeviceList.size();
    }

    public interface ItemClickListener {
        void itemClick(BluetoothDevice device);

        void bondClick(BluetoothDevice position);

        void connectClick(BluetoothDevice position);
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout device_layout;
        TextView device_name_tv;
        TextView mac_id_tv;
        Button connect_device_btn;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            device_layout = itemView.findViewById(R.id.device_layout);
            device_name_tv = itemView.findViewById(R.id.device_name_tv);
            mac_id_tv = itemView.findViewById(R.id.mac_id_tv);
            connect_device_btn = itemView.findViewById(R.id.connect_device_btn);
        }
    }
}
